package net.melon.product.inst.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.esj.basic.cache.AbstractCacheProxy;
import net.esj.basic.exception.MelonException;
import net.esj.basic.utils.Validators;
import net.melon.product.inst.service.ClassifyService;
import net.melon.product.pojo.ProductClassify;

import org.springframework.stereotype.Service;

@Service("classifyCacheProxy")
public class ClassifyCacheProxy  extends AbstractCacheProxy<List<ProductClassify>> implements ClassifyService {

	public static String CLASSIFY_CACHE = "_classify_cache_";
	
	private static Map<Long,ProductClassify> pcMap = new HashMap<Long,ProductClassify>(); 
 	
	@Resource
	private ClassifyService classifyService;
	
	
	public ClassifyCacheProxy() {
		super(CLASSIFY_CACHE);
	}

	@Override
	public ProductClassify saveClassify(ProductClassify entity) {
		ProductClassify classify = classifyService.saveClassify(entity);
		cleanCache();
		return classify;
	}

	@Override
	public ProductClassify updateClassify(ProductClassify entity) {
		ProductClassify classify = classifyService.updateClassify(entity);
		cleanCache();
		return classify;
	}

	@Override
	public ProductClassify getClassify(Long id) {
		List<ProductClassify> list = this.cache.get(CLASSIFY_CACHE);
		if(list==null){
			getProductClassifyList(new Object[]{1,0});
		}
		ProductClassify classify = pcMap.get(id);
		return classify;
	}

	@Override
	public void deleteClassify(Long id) {
		if(checkDeleteClassify(id)){
			classifyService.deleteClassify(id);
			cleanCache();
		}else{
			throw new MelonException("该类型下有子类型或者有产品不能删除");
		}
	}
	
	@Override
	public List<ProductClassify> getProductClassifyList(Object[] objectArr) {
		List<ProductClassify> list = this.cache.get(CLASSIFY_CACHE);
		if(list==null){
			list = classifyService.getProductClassifyList(objectArr);
			this.cache.put(CLASSIFY_CACHE, list);
			if(!Validators.isEmpty(list)){
				for(ProductClassify pc : list){
					pcMap.put(pc.getId(),pc);
				}
			}
		}
		return list;
	}
	
	public List<ProductClassify> getClassifyParents(ProductClassify classify) {
		List<ProductClassify> list = new ArrayList<ProductClassify>();
		list.add(classify);
		while(classify.getParent()!=null){
			classify = pcMap.get(classify.getParent().getId());
			list.add(classify);
		}
		return list;
	}

	private void cleanCache(){
		this.cache.remove(CLASSIFY_CACHE) ;
	}

	@Override
	public boolean checkDeleteClassify(Long id) {
		if(getChildCountByClassify(id) && classifyService.checkDeleteClassify(id)){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean getChildCountByClassify(Long id) {
		List<ProductClassify> list = getProductClassifyList(new Object[]{1,0});
		for(ProductClassify pc : list){
			if(pc.getParent()!=null && pc.getParent().getId() == id){
				return false;
			}
		}
		return true;
		
	}

	public List<ProductClassify> getProductClassifyList() {
		return getProductClassifyList(new Object[]{1,0});
	}
}
